package com.egg.ylt.presenter.impl;

import android.util.Log;

import com.egg.ylt.Utils.API;
import com.egg.ylt.pojo.NatatoriumByidEntity;
import com.egg.ylt.pojo.RealtimeDataEntity;
import com.egg.ylt.presenter.IShopDetailsPresenter;
import com.egg.ylt.view.IShopDetailsView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by sichaoqi on 2018/9/3.
 * Modify by dlx on 2018/12/5
 */

public class ShopDetailsPresenterImpl extends BasePresenter<IShopDetailsView> implements IShopDetailsPresenter {

    private Timer timer;
    private TimerTask timerTask;
    private boolean isFirst = true;

    @Override
    public void getShopDetails(String token, String id, String pageSize, String pageNum, String companyId) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("id", id);
        hashMap.put("pageSize", pageSize);
        hashMap.put("pageNum", pageNum);
        hashMap.put("companyId", companyId);
        RequestManager.getInstance().requestPostByAsyn(API.GET_NATATORIUM_BY_ID, hashMap,
                new ReqCallBack<NatatoriumByidEntity>() {

            @Override
            public void onReqSuccess(NatatoriumByidEntity result) {
//                mView.delayInflateViews();
                mView.dismissDialogLoading();
                mView.getShopData(result);
            }

            @Override
            public void onFailure(String result) {
//                mView.delayInflateViews();
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
//                mView.delayInflateViews();
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void loop2GetShopAirWaterStatus(final String companyId, final String shopId) {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                getShopAWS(companyId, shopId);
            }
        };
        timer.schedule(timerTask, 1000, 30 * 1000);
    }

    @Override
    public void onDestroy() {
        timer.cancel();
    }

    @Override
    public void setCancelMessage(String token, String shopId) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("messageId", shopId);
        RequestManager.getInstance().requestPostByAsyn(API.POST_CANCEL_MESSAGE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.cancelCollect();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void setCollectMessage(String token, String shopId, String companyId) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("messageId", shopId);
        hashMap.put("companyId", companyId);
        //0 为咨询标识，1为门店详情
        hashMap.put("collectType", "1");
        RequestManager.getInstance().requestPostByAsyn(API.POST_COLLECT_MESSAGE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.doCollect();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });

    }

    private void getShopAWS(String companyId, String shopId) {
        HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("shopId", shopId);
        hashMap.put("companyId", companyId);
        RequestManager.getInstance().requestPostByAsyn(API.POST_REALTIME_DATA, hashMap,
                new ReqCallBack<RealtimeDataEntity>() {

                    @Override
                    public void onReqSuccess(RealtimeDataEntity result) {
                        if (isFirst) {
                            isFirst = false;
                            mView.delayInflateAirWaterViews();
                        }
                        mView.showAirWaterStatus(result);
                    }

                    @Override
                    public void onFailure(String result) {
//                mView.showError(result);
                        CommonUtils.makeEventToast(mContext, result, false);
                    }

                    @Override
                    public void onReqFailed(ErrorBean error) {
//                mView.showException(error.toString());
                        CommonUtils.makeEventToast(mContext, error.getMsg(), false);
                    }
                });
    }
}
